package com.baizhi.controller;

import com.baizhi.entity.Comment;
import com.baizhi.entity.User;
import com.baizhi.entity.Video;
import com.baizhi.entity.VideoDetailVO;
import com.baizhi.entity.VideoVO;
import com.baizhi.feignclients.UserClients;
import com.baizhi.service.VideoService;
import com.baizhi.utils.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 视频(Video)表控制层
 *
 * @author makejava
 * @since 2024-02-18 21:04:45
 */
@Slf4j
@RestController
public class VideoController {

    /**
     * 服务对象
     */
    @Resource
    private VideoService videoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserClients userClients;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Video> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.videoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param video 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Video> add(Video video) {
        return ResponseEntity.ok(this.videoService.insert(video));
    }

    /**
     * 编辑数据
     *
     * @param video 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Video> edit(Video video) {
        return ResponseEntity.ok(this.videoService.update(video));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.videoService.deleteById(id));
    }

    /**
     * 查看分类下视频列表
     * @param page
     * @param rows
     * @param categoryId
     * @return
     */
    @GetMapping("/videos")
    public List<VideoVO> findAllByCategoryId(@RequestParam(value = "",defaultValue = "1") Integer page,
                                             @RequestParam(value = "",defaultValue = "1") Integer rows,
                                             Integer categoryId){

        log.info("当前页:{},每页显示记录数:{},分类id:{}",page,rows,categoryId);
        List<VideoVO> videoVOS = videoService.findAllByCategoryId(page, rows, categoryId);
        log.info("符合条件的记录总数:{}",videoVOS.size());

        return videoVOS;
    }

    /**
     * 查询视频详情
     * @param videoId
     * @param token
     * @return
     */
    @GetMapping("/videos/{id}")
    public VideoDetailVO video(@PathVariable("id") String videoId, String token){

        log.info("当前接收到的videoId: {}",videoId);
        VideoDetailVO videoDetailVO = videoService.queryDetailById(videoId, token);
        log.info("查询到的视频详细对象信息: {}", JSONUtils.writeValueAsString(videoDetailVO));
        return videoDetailVO;
    }


    /**
     * 视频推荐
     * @param page
     * @param per_page
     * @return
     */
    @GetMapping("recommends")
    public List<VideoVO> recommends(@RequestParam("page") Integer page ,@RequestParam("per_page") Integer per_page){

        List<VideoVO> videoVOS = videoService.queryAllByLimit(page,per_page);
        log.info("视频推荐列表数量:{}",videoVOS.size());
        return videoVOS;
    }

    /**
     * 评论信息
     */
    @GetMapping("/videos/{videoId}/comments")
    public Map<String, Object> comments(
            @PathVariable("videoId") Integer videoId,
            @RequestParam("page") Integer page,
            @RequestParam("per_page") Integer rows, HttpServletRequest request) {
        log.info("视频id: {}", videoId);
        log.info("当前页: {}", page);
        log.info("每页显示记录数: {}", rows);
        //调用用户微服务
        return userClients.comments(videoId, page, rows);
    }

    /**
     * 视频评论
     */
    @PostMapping("/videos/{videoId}/comments")
    public void comments(@PathVariable("videoId") Integer videoId, @RequestBody Comment comment, HttpServletRequest request) {
        //1.获取token
        String token = request.getParameter("token");
        //2.根据tokenKey获取用户信息
        String tokenKey = "session_" + token;
        //3.获取redis登陆用户信息
        User user = (User) redisTemplate.opsForValue().get(tokenKey);

        log.info("token为: {}", token);
        log.info("评论的视频id: " + videoId);
        log.info("评论对象内容: " + JSONUtils.writeValueAsString(comment));
        log.info("当前评论的用户: " + JSONUtils.writeValueAsString(user));

        //4.调用用户服务视频评论功能
        userClients.comments(user.getId(), videoId, comment);
    }

}

